package cn.net.dpi.wlqc.server.action;

import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import cn.net.dpi.wlqc.server.handler.Request;
import cn.net.dpi.wlqc.server.handler.Response;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 翼卡图片上传处理
 *
 * @author Looly
 */
@Slf4j
public class ECarPictureUploadAction implements Action {

    private final ObjectMapper objectMapper = SpringUtil.getBean(ObjectMapper.class);

    @Override
    public void doAction(Request request, Response response) {
        FullHttpRequest r = (FullHttpRequest) request.getNettyRequest();
        ByteBuf content = r.content();
        String jsonStr = content.toString(CharsetUtil.UTF_8); // 获取JSON字符串
        try {
            JsonNode jsonNode = objectMapper.readTree(jsonStr);
            log.info("jsonNode:{}", jsonNode);
        } catch (Exception e) {
        }
        ObjectNode objectNode = JsonNodeFactory.instance.objectNode();
        objectNode.put("success", true);
        objectNode.put("message", "成功");
        try {
            response.setJsonContent(objectMapper.writeValueAsString(objectNode));
        } catch (Exception e) {
            //ignore
        }
    }

}